import React from 'react';
import { Button, Form, message } from 'antd';
import './index.css';
import { axhttp } from '@/utils/axhttp';
import { AxInput, AxPassword, AxSelectEnum } from '@/components/AxForm/AxFormItem';
import { axdatehelper } from '@/shared/axhelper';

const { postFormXhr } = axhttp;
const Index: React.FC<any> = props => {
  const { form } = props;

  const loginSubmit = (e: any) => {
    e && e.preventDefault();

    form.validateFieldsAndScroll((err: any, values: any) => {
      if (!err) {
        var formValue = form.getFieldsValue();
        postFormXhr('/api/user/create', {
          ...formValue,
          name: formValue.name,
          passWord: formValue.passWord,
          createTime: axdatehelper.Dateformat(new Date(), 'yyyy-MM-dd HH:mm:ss'),
        }).then(res => {
          message.success('注册成功');
          window.location.href = '/#/login';
        });
      }
    });
  };

  return (
    <div className="ax-register">
      <h1>用户注册</h1>
      <AxInput form={form} label="用户名" field="name" required />
      <AxSelectEnum form={form} label="性别" field="sex" enumName="sex" required />
      <AxInput form={form} label="身份证" field="idCard" required />
      <AxPassword form={form} label="请设置密码" field="passWord" required />
      <AxPassword form={form} label="请确认密码" field="passWord" required />
      <AxInput form={form} label="电话" field="mobile" />
      <Button type="primary" onClick={loginSubmit} className="ax-register-button">
        注册
      </Button>
    </div>
  );
};

export default Form.create<any>()(Index);
